// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="CSLib.Net Team">
//   Copyright (c) Roman "4ux-nbIx" Novitsky 2010
// </copyright>
// <summary>
//   Defines String class extension methods.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSLib.Net.Extensions
{
  #region Namespace Imports

  using System;
  using System.Diagnostics.CodeAnalysis;
  using System.Globalization;
  using System.Linq;

  using Annotations;

  using Diagnostics;

  #endregion


  /// <summary>
  /// Defines String class extension methods.
  /// </summary>
  [UsedImplicitly(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.WithMembers)]
  public static class StringExtensions
  {
    #region Public Methods

    /// <summary>
    /// Determines whether the specified string value is <c>null</c> or white space.
    /// </summary>
    /// <param name="value">
    /// The value.
    /// </param>
    /// <returns>
    /// <c>true</c> if the specified string value is <c>null</c> or white space; otherwise, <c>false</c>.
    /// </returns>
    [AssertionMethod]
    public static bool IsNullOrWhiteSpace(
      [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this string value)
    {
#if WINDOWS
      return string.IsNullOrWhiteSpace(value);
#else
      return value == null || value.All(t => char.IsWhiteSpace(t));
#endif
    }


    /// <summary>
    /// Replaces the format item in a specified string with the string
    /// representation of a corresponding object in a specified array using
    /// invariant culture.
    /// </summary>
    /// <param name="format">A composite format string.</param>
    /// <param name="arguments">An object array that contains zero or more
    /// objects to format.</param>
    /// <returns>
    /// A copy of format in which the format items have been replaced by the
    /// string representation of the corresponding objects in arguments.
    /// </returns>
    [StringFormatMethod("format")]
    public static string FormatInvariant([NotNull] this string format, [CanBeNull] params object[] arguments)
    {
      Assert.ArgumentNotNull(format, "format");

      return string.Format(CultureInfo.InvariantCulture, format, arguments);
    }


    /// <summary>
    /// Replaces the format item in a specified string with the string
    /// representation of a corresponding object in a specified array. A
    /// specified parameter supplies culture-specific formatting information.
    /// </summary>
    /// <param name="format">A composite format string.</param>
    /// <param name="formatProvider">An object that supplies culture-specific
    /// formatting information.</param>
    /// <param name="arguments">An object array that contains zero or more
    /// objects to format.</param>
    /// <returns>
    /// A copy of format in which the format items have been replaced by the
    /// string representation of the corresponding objects in arguments.
    /// </returns>
    [StringFormatMethod("format")]
    public static string Format(
      [NotNull] this string format, [NotNull] IFormatProvider formatProvider, [CanBeNull] params object[] arguments)
    {
      Assert.ArgumentNotNull(format, "format");
      Assert.ArgumentNotNull(formatProvider, "formatProvider");

      return string.Format(formatProvider, format, arguments);
    }

    #endregion
  }
}